package hust.rpc.pojo;

import hust.rpc.utils.CommonUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Channel {

    private Host host;
    private io.netty.channel.Channel channel;

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Channel)) {
            return false;
        }
        Channel channel = (Channel) obj;
        return getHost().equals(channel.getHost());
    }

    @Override
    public int hashCode() {
        return getHost().hashCode();
    }

    public static Channel wrap(io.netty.channel.Channel channel) {
        return new Channel(CommonUtils.channelToHost(channel), channel);
    }

    public static Channel wrap(Host host) {
        Channel channel = new Channel();
        channel.setHost(host);
        return channel;
    }

    @Override
    public String toString(){
        return host.toString();
    }
}
